
package com.hitrust.gs.util;

import java.util.*;

public class ParameterFactory 
{
	public Vector mParams = new Vector();
	private String mSourceStr = "";
	
	public ParameterFactory()
	{
	}
	
	public ParameterFactory(String str) throws Exception
	{
		setSourceStr(str);
		parse(str);
	}
	
	private static boolean isSpecChar(char ch)
	{
		return ch=='\\' || ch=='=' || ch=='&' ;
	}
	
	//验证字符，字符不能是 "=" 或者是 "&" 
	
	private static boolean isValidChar(char ch)
	{
		return ch!='=' && ch!='&' ;
	}

	private static String convert(String value)
	{
		if(null==value||value.equalsIgnoreCase("")) return "" ;
		String ret = "" ;
		char ch ;
		for(int i=0;i<value.length();i++)
		{
			ch = value.charAt(i);
			if(isSpecChar(ch))
			{
				ret += "\\" + ch ;
			}
			else
			{
				ret += ch ;
			}
		}
		return ret ;
	}
	public void addParam(String name,String value) throws Exception
	{
		// name 和 value 不能为null
		if( name == null ) throw new Exception("Bad name = null");
		if( value == null ) throw new Exception("Bad value = null");
		mParams.add(new NamedValue(name,value));
	}
	
	// 根据设置的name和value参数，得到字符串流
	public String getStrStream() throws Exception
	{
		String ret = "";
		for(int i=0; i<mParams.size(); i++)
		{
			NamedValue nv = (NamedValue)mParams.get(i);
			ret += convert(nv.getName()) + "=" + convert(nv.getValue()) + "&";
		}	
		return ret;
	}
	private String print() throws Exception
	{
		String ret = "";
		for(int i=0; i<mParams.size(); i++)
		{
			NamedValue nv = (NamedValue)mParams.get(i);
			ret += nv.getName() + "   " + nv.getValue() + "\n";
		}	
		return ret;
	}
	
	// 解析socket传过来的字符串...
	private void parse(String values) throws Exception
	{
		String name = "";
		String value = "";
		char ch ;
		int state = 0 ;    //start
		if(null==values) throw new Exception("Bad parameters=null"); 
		for(int i=0;i<values.length();i++)
		{
			ch = values.charAt(i);
			if(state==0)             //start,to get name
			{
				if(ch=='\\')
				{
					i++ ;
					name += values.charAt(i);
					state = 1 ;
					continue ;
				}
				if(!isValidChar(ch))
				{
					throw new Exception("Bad values="+values);
				}
				name += ch ; 
				state = 1;  
				continue;
			}
			else if(state==1)  //getting name
			{
				if(ch=='\\')
				{
					i++ ;
					name += values.charAt(i);
					continue ;
				}
				if(isValidChar(ch))
				{
					name += ch ;
					continue;			
				}
				if(ch=='=')
				{
					state =  2 ; 
					continue;
				}
				throw new Exception("Bad values="+values); 
			}
			else if(state==2)   //to get value 
			{ 
				if(ch=='\\')
				{
					i++ ;
					value += values.charAt(i);
					state = 3 ;
					continue ;
				}
				if(isValidChar(ch))
				{
					value += ch ;
					state = 3 ;
					continue;
				}
				if(ch=='&')
				{
					mParams.add(new NamedValue(name,value));
					name = "";
					value = "";
					state = 0 ;
					continue;
				}
				throw new Exception("Bad values="+values);  
			}
			else if(state==3) // getting value
			{
				if(ch=='\\')
				{
					i++ ;
					value += values.charAt(i);
					continue ;
				}
				if(isValidChar(ch))
				{
					value += ch ;
					continue;
				}
				if(ch=='&')
				{
					mParams.add(new NamedValue(name,value));
					name = "";
					value = "";
					state = 0 ;
					continue;
				}
				throw new Exception("Bad values="+values);  
			}
		}
		if(state==0)  // to get name 
		{
	
		}
		else if(state==1) // getting name
		{
			throw new Exception("Bad values="+values);
		}
		else if(state==2) // to get value
		{
			mParams.add(new NamedValue(name,""));
		}
		else if(state==3) // getting value
		{
			mParams.add(new NamedValue(name,value));
		}
	}
	public String getString(String name) throws Exception
	{
		for(int i=0;i<mParams.size();i++)
		{
			NamedValue nv = (NamedValue) mParams.get(i); 
			if(nv.getName().equalsIgnoreCase(name))
			{
				return nv.getValue();
			}
		}
		return null ;
	}	
	public int getInt(String name) throws Exception
	{
		try
		{
			return Integer.parseInt(getString(name));
		}
		catch(NumberFormatException e)
		{
			throw new Exception("错误的参数("+name+"),值为("+getString(name)+")");
		}
	}
	public int getInt(String name,int defValue)
	{
		int ret ;
		try
		{
			ret = getInt(name);
		}
		catch(Exception e)
		{
			ret = defValue;
		}
		return ret  ;
	}
	public double getDouble(String name) throws Exception
	{
		try
		{
			return Double.parseDouble(getString(name));
		}
		catch(NumberFormatException e)
		{
			throw new Exception("错误的参数("+name+"),值为("+getString(name)+")");
		}
	}
	public double getDouble(String name,double defValue)
	{
		double ret ;
		try
		{
			ret = getDouble(name);
		}
		catch(Exception e)
		{
			ret = defValue;
		}
		return ret  ;
	}
	
	// add by daniel start


	public void clear()
	{
		mParams.clear();
	}
	// add by daniel end

	public String getSourceStr()
	{
		return mSourceStr;
	}
	public void setSourceStr(String sourceStr)
	{
		mSourceStr = sourceStr;
	}
}
